import md5 from 'crypto-js/md5';
import { codeToText } from "element-china-area-data";

const TokenKey = 'zh-token'
const UserID = 'userId';

export function getToken() {
    return localStorage.getItem(TokenKey);
}

export function setToken(token) {
    return localStorage.setItem(TokenKey, token);
}

export function removeToken() {
    return localStorage.removeItem(TokenKey);
}

export function getUserId() {
    return localStorage.getItem(UserID);
}
export function setUserId(id) {
    return localStorage.setItem(UserID, id);
}
//加密平台标识
const OS = 'web';

//加密盐
const SALT = 'emhhHaC';

//获取请求头
export const getHeader = () => {
    const timeStamp = Date.now() + '';
    const sign = '01' + md5(timeStamp + SALT);
    let obj = {
        'os': OS,
        'x-time': timeStamp,
        'x-sign': sign,
    };
    if (getToken()) {
        obj['token'] = getToken();
    }
    if (getUserId()) {
        obj['userId'] = getUserId();
    }
    return obj;
}

//把区域码转成汉字
export const getCodeToText = (codeArray) => {
    let area = [];
    switch (codeArray.length) {
        case 1:
            area.push(codeToText[codeArray[0]])
            break;
        case 2:
            area.push(codeToText[codeArray[0]])
            area.push(codeToText[codeArray[1]])
            break;
        case 3:
            area.push(codeToText[codeArray[0]])
            area.push(codeToText[codeArray[1]])
            area.push(codeToText[codeArray[2]])
            break;
        default:
            break;
    }
    return area;
}


//上传图片地址
export const uploadUrl = 'https://api.zhgylgl.com/api/uploadImg';

//深克隆方法
export const deepClone = (obj) => {
    let objClone = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object") {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                //判断ojb子元素是否为对象，如果是，递归复制
                if (obj[key] && typeof obj[key] === "object") {
                    objClone[key] = deepClone(obj[key]);
                } else {
                    //如果不是，简单复制
                    objClone[key] = obj[key];
                }
            }
        }
    }
    return objClone;
}

//返回一个秒为单位的时间戳 ,转换为2024/01/01 08:00:00
export function transformDate(time) {
    const timestamp = time;
    const date = new Date(timestamp * 1000);
    return date.toLocaleString();
}


//下载后端返回的文件
export const downloadFile = (url) => {
    var oReq = new XMLHttpRequest();
    // The Endpoint of your server
    const fileName = (url + '').replace('http://img.zhgylgl.com/', '');
    var URLToPDF = url;
    // Configure XMLHttpRequest
    oReq.open("GET", URLToPDF, true);
    // Important to use the blob response type
    oReq.responseType = "blob";
    // When the file request finishes
    // Is up to you, the configuration for error events etc.
    oReq.onload = function () {
        // Once the file is downloaded, open a new window with the PDF
        // Remember to allow the POP-UPS in your browser
        var file = new Blob([oReq.response], {
            type: "application/pdf"
        });
        // Generate file download directly in the browser !
        // saveAs(file, "mypdffilename.pdf");
        saveAs(file, decodeURIComponent(fileName));
    };
    oReq.send();
}
